package com.lancoo.znbkxx.teacher.diagnosis

import android.annotation.SuppressLint
import androidx.recyclerview.widget.LinearLayoutManager
import com.lancoo.znbkxx.teacher.Ext
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.adapter.SpacesItemDecoration
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.MyToast
import com.lancoo.znbkxx.teacher.base.BaseActivity
import com.lancoo.znbkxx.teacher.model.data.TopicZsd
import com.lancoo.znbkxx.teacher.net.BkApiMethods
import com.lancoo.znbkxx.teacher.utils.DensityUtils
import com.lancoo.znbkxx.teacher.utils.HtmlUtils
import com.lancoo.znbkxx.teacher.utils.LancooUtils
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import kotlinx.android.synthetic.main.activity_vocablist.*
import java.net.ConnectException
import java.net.SocketTimeoutException
import java.util.concurrent.TimeoutException

/**
 *@author Admin
 *@date 2021/1/4
 *
 */
class GrammarListActivity : BaseActivity() {
    private val zsd by lazy { intent.getStringExtra("zsd") }
    private val type by lazy { intent.getIntExtra("type", 0) }
    private val stuID by lazy { intent.getStringExtra("stuID") }
    private lateinit var adapter: StuTopicInfoListAdapter
    private val grammarList = mutableListOf<TopicZsd>()
    private var totalSize = 0
    private var page = 1
    override fun setLayout(): Int {
        return R.layout.activity_vocablist
    }

    override fun initView() {
        iv_back.setOnClickListener { finish() }
        tv_title.text = when (type) {
            0 -> "熟练掌握语法"
            1 -> "薄弱语法"
            2 -> "已掌握语法"
            else -> "总语法"
        }
        adapter = StuTopicInfoListAdapter(grammarList)
        adapter.openLoadAnimation()
        rv_vocab.layoutManager = LinearLayoutManager(this)
        rv_vocab.adapter = adapter
        val hashMap = hashMapOf<String, Int>()
        hashMap[SpacesItemDecoration.TOP_SPACE] = DensityUtils.dp2px(this, 10f)
        rv_vocab.addItemDecoration(SpacesItemDecoration(hashMap))
        swipe_layout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                grammarList.clear()
                getGrammarList()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                if (grammarList.size <= totalSize) {
                    page += 1
                }
                getGrammarList()
            }
        })
        getGrammarList()
    }

    override fun isNeedToolBar(): Boolean {
        return false
    }

    @SuppressLint("CheckResult", "SetTextI18n")
    fun getGrammarList() {
        val method = BkApiMethods(this)
        val params = BkApiMethods.Paramas()
        params["ZsdArea"] = zsd
        params["MarkSign"] = if (type == 2) 0 else type
        params["Page"] = page
        params["stuID"] = stuID
        method.getStuTopicInfoList(Ext.RZZD_URL, params).subscribe({
            if (it.Code == 1) {
                totalSize = it.Data.Total
                tv_list_title.text = HtmlUtils.setText(
                        "${
                        when (type) {
                            0 -> "熟练掌握"
                            1 -> "薄弱"
                            2 -> "已掌握"
                            else -> "总"
                        }
                        }语法<font color='#22d7bb'>${totalSize}</font>个，" + "其中常考语法占<font color='#2dbcff'>${
                        (it.Data.FirstMix * 100).toInt()
                        }%</font>，" + "次常考词汇占<font color='#ffc49d'>${
                        (it.Data.SecondMix * 100).toInt()
                        }%</font>，" + "不常考语法占<font color='#ff8888'>${
                        (it.Data.ThirdMix * 100).toInt()
                        }%</font>"
                )
                grammarList.addAll(it.Data.TopicZsdLists)
                adapter.setNewData(grammarList)
                swipe_layout.finishRefresh()
                swipe_layout.setEnableLoadMore(grammarList.size < totalSize)
                if (grammarList.size == totalSize) {
                    swipe_layout.finishLoadMoreWithNoMoreData()
                } else {
                    swipe_layout.finishLoadMore()
                }
            } else {
                MyToast.showToast(it.Message)
            }
        }, {
            val error: String =
                    if (it is TimeoutException || it is SocketTimeoutException || it is ConnectException) {
                        getString(R.string.aibk_network_timeout)
                    } else {
                        getString(R.string.aibk_server_sick)
                    }
            MyToast.showToast( error)
        })
    }
}